﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.member.money
{
    public partial class WithDrawResult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPostParams();
            }
        }

        private void InitPostParams()
        {
            GoodResultDiv.Visible = false ;
            BadResultDiv.Visible =  true;

            string curUserId = Request.Form["curuserid"];
            string withDrawAmount = Request.Form["withDrawAmount"];
            if (curUserId != null&& curUserId !=string.Empty  && withDrawAmount != null && curUserId ==AliApi.appUserId )
            {
                DateTime now = DateTime.Now ;
                string filter = "cbuserid='' and WithDrawDate="+now.ToShortDateString ();
                cbWithDraw cbwithdraw = cbWithDraw.Find(filter );
                if (cbwithdraw.Count >= 3)
                {
                    GoodResultDiv.Visible = false;
                    BadResultDiv.Visible = true;
                }
                else
                {
                    cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
                    if (user.Count > 0 && user.CheckBankInfo ()==true )
                    {
                        try
                        {

                            string newGuid =  Guid.NewGuid().ToString("N");

                            double amount = Convert.ToDouble(withDrawAmount);
                            user.cbmoney.Value -= amount;
                            user.cbmoneyD.Value += amount;
                            user.Modify();

                            cbwithdraw.New();
                            cbwithdraw.cbuserid.Value = curUserId;
                            cbwithdraw.WithDrawId.Value = newGuid;
                            cbwithdraw.WithDrawTime.StringValue = now.ToLongTimeString(); ;
                            cbwithdraw.WithDrawDate.Value = now;
                            cbwithdraw.Amount.Value = amount;
                            cbwithdraw.WithDrawIpAddr.Value = Request.UserHostAddress;
                            cbwithdraw.WithDrawStatus.Value = RuleCode.Enum.cbWithDrawStatus.Applicated;
                            cbwithdraw.Notes.Value = "";
                            cbwithdraw.BankAccount.Value = user.BankAccount.Value;
                            cbwithdraw.BankAccountName.Value = user.BankAccountName.Value;
                            cbwithdraw.BankAccountType.Value = user.BankAccountType.Value;
                            cbwithdraw.BankBranchName.Value = user.BankBranchName.Value;
                            cbwithdraw.BankName.Value = user.BankName.Value;
                            cbwithdraw.Insert();

                            cbwithdraw .CurrentIndex =0;

                            cbmoneytrans moneytrans = cbmoneytrans.Find("1=2");

                            moneytrans.New();
                            moneytrans.cbuserid.Value = AliApi.appUserId;
                            moneytrans.cbmoney.Value = -amount ;
                            moneytrans.cbmoneytrantype.Value = MONEY_TransType.WithDraw ;
                            moneytrans.jccreatedate.Value = now;
                            moneytrans.jccreatetime.StringValue = now.ToLongTimeString();
                            moneytrans.jcdesc.Value = cbwithdraw.WithDrawId .ToString() + " " + user.cbuserid .ToString();
                            moneytrans.NoYes_dongjie.Value = RuleCode.Enum.NoYes.No;
                            moneytrans.RefTable.Value = "cbWithDraw";
                            moneytrans.RefRecID.Value = cbwithdraw.RECID.ToString ();
                            moneytrans.Insert();

                            moneytrans.New();
                            moneytrans.cbuserid.Value = AliApi.appUserId;
                            moneytrans.cbmoney.Value = amount;
                            moneytrans.cbmoneytrantype.Value = MONEY_TransType.Block ;
                            moneytrans.jccreatedate.Value = now;
                            moneytrans.jccreatetime.StringValue = now.ToLongTimeString();
                            moneytrans.jcdesc.Value = cbwithdraw.WithDrawId.ToString() + " " + user.cbuserid.ToString();
                            moneytrans.NoYes_dongjie.Value = RuleCode.Enum.NoYes.Yes ;
                            moneytrans.RefTable.Value = "cbWithDraw";
                            moneytrans.RefRecID.Value = cbwithdraw.RECID.ToString();
                            moneytrans.Insert();
                            

                            GoodResultDiv.Visible = true;
                            BadResultDiv.Visible = false;
                        }
                        catch { }

                        return;
                    }
                }
            }
            
            MessageBox.ShowAndRedirect(this.Page, "对不起，您没有权限访问此页面！", ResolveUrl ("~/default.aspx"));
            
        }
    }
}
